<?php

if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}
require_once __DIR__ . '/Config.php';

use OSS\OssClient;
use OSS\Core\OssException;

/**
 * Class Common
 *
 * The Common class for 【Samples/*.php】 used to obtain OssClient instance and other common functions
 */
class Common
{
    const endpoint = Config::OSS_ENDPOINT;
    const accessKeyId = Config::OSS_ACCESS_ID;
    const accessKeySecret = Config::OSS_ACCESS_KEY;
    const bucket = Config::OSS_TEST_BUCKET;

    /**
     * Get an OSSClient instance according to config.
     *
     * @return OssClient An OssClient instance
     */
    public static function getOssClient()
    {
        try {
            $ossClient = new OssClient(self::accessKeyId, self::accessKeySecret, self::endpoint, false);
        } catch (OssException $e) {
            printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
            printf($e->getMessage() . "\n");
            return null;
        }
        return $ossClient;
    }

    public static function getEndPoint()
    {
        return self::endpoint;
    }

    public static function getAccessKeyId()
    {
        return self::accessKeyId;
    }

    public static function getAccessKeySecret()
    {
        return self::accessKeySecret;
    }

    public static function getBucketName()
    {
        return self::bucket;
    }

    /**
     * A tool function which creates a bucket and exists the process if there are exceptions
     */
    public static function createBucket()
    {
        $ossClient = self::getOssClient();
        if (is_null($ossClient)) exit(1);
        $bucket = self::getBucketName();
        $acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
        try {
            $ossClient->createBucket($bucket, $acl);
        } catch (OssException $e) {

            $message = $e->getMessage();
            if (\OSS\Core\OssUtil::startsWith($message, 'http status: 403')) {
                echo "Please Check your AccessKeyId and AccessKeySecret" . "\n";
                exit(0);
            } elseif (strpos($message, "BucketAlreadyExists") !== false) {
                echo "Bucket already exists. Please check whether the bucket belongs to you, or it was visited with correct endpoint. " . "\n";
                exit(0);
            }
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }

    public static function println($message)
    {
        if (!empty($message)) {
            echo strval($message) . "\n";
        }
    }

    public static function putObject($content,$object){
        $ossClient = self::getOssClient();
        if (is_null($ossClient)) exit(1);
        $accessKeyId = self::getAccessKeyId();;
        $accessKeySecret = self::getAccessKeySecret();
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = self::getEndPoint();
        // 存储空间名称
        $bucket= self::getBucketName();
        // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->putObject($bucket, $content, $object);
            return true;
        } catch (OssException $e) {
            return false;
        }
    }
      public static function putObjectFile($content,$object){

          ini_set('display_errors','on');
          error_reporting(E_ALL);
            $ossClient = self::getOssClient();
            if (is_null($ossClient)) exit(1);
            $accessKeyId = self::getAccessKeyId();;
            $accessKeySecret = self::getAccessKeySecret();
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            $endpoint = self::getEndPoint();
            // 存储空间名称
            $bucket= self::getBucketName();
            // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg
            try {
                $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
                $result = $ossClient->uploadFile($bucket, $object, $content);
                return $result;
            } catch (OssException $e) {
                return false;
            }
        }

}

# Common::createBucket();
